#!/bin/bash
#
# Copyright (c) 2007 OLPC
# Author: gxlxnis@lxptop.org | sed s/x/a/ 
# Version 0.6
#
# this program is free software; you can redistribute it and/or modify
# it under the terms of the gnu general public license as published by
# the free software foundation; either version 2 of the license, or
# (at your option) any later version.
#
# this program is distributed in the hope that it will be useful,
# but without any warranty; without even the implied warranty of
# merchantability or fitness for a particular purpose.  see the
# gnu general public license for more details.
#
# you should have received a copy of the gnu general public license
# along with this program; if not, write to the free software
# foundation, inc., 51 franklin st, fifth floor, boston, ma  02110-1301  usa

function info {
echo
echo "Model     : "`cat /ofw/model`
echo "Serial    : "`cat /ofw/serial-number`
echo "MAC       : "`ifconfig eth0|awk '/^eth0/ {print $5}'`
echo
echo "Build     : "`cat /boot/olpc_build`
echo "Firmware  : "`cat /ofw/openprom/model`
echo "Libertas  : "`ethtool -i eth0|awk '/firmware/ {print $2}'`
echo
echo "Nick      : "`cat /home/olpc/.sugar/default/config|grep ^nick|sed 's/^[^=]*= //'`
echo "Uptime    : "`uptime|awk '{print $1}'`
}

function net {

echo
#ip
for i in `ifconfig|grep HWaddr|awk '{print $1}'`
do
	echo -n "IP $i   : "
	echo `ifconfig $i|grep "inet addr"|awk 'BEGIN{FS="addr:"}{print $2}'|awk '{print $1}'` 
done

#nameserver
dns=''
dns=`cat /etc/resolv.conf|grep "^name"|awk 'NR==1 {print $2}'`
echo 'DNS       : '$dns
echo ''


#telepathy
sugar-telepathies|sed -e 's/0//' -e 's/1/(connecting)/' -e 's/2/(disconnected)/'|
sed 's/^.*$/            &/'|sed '1 s/            /Telepathy : /'

#jabber
jabber=''
jabber=`netstat -tpT --numeric-ports 2> /dev/null|grep telepathy|grep ESTABLISHED|awk '{print $5}'|awk 'BEGIN{FS=":522"}{print $1}'`
echo 'Jabber    : '$jabber

#xos
echo "XOs       : "`sugar-xos|wc -l`
echo

#essid
essid=''
essid=`iwconfig eth0|grep ESSID|awk 'BEGIN{FS="ESSID:\""}{print $2}'|awk 'BEGIN{FS="\""}{print $1}'`
echo 'Essid     : '$essid


#channel
ch=''
freq=`iwconfig eth0|grep Freq|grep -v eth0|awk 'BEGIN{FS="uency:"}{print $2}'|awk '{print $1}'`
[ "$freq" = 2.412 ] && ch=1
[ "$freq" = 2.437 ] && ch=6
[ "$freq" = 2.462 ] && ch=11

[ "$freq" = 2.417 ] && ch=2
[ "$freq" = 2.422 ] && ch=3
[ "$freq" = 2.427 ] && ch=4
[ "$freq" = 2.432 ] && ch=5
[ "$freq" = 2.442 ] && ch=7
[ "$freq" = 2.447 ] && ch=8
[ "$freq" = 2.452 ] && ch=9
[ "$freq" = 2.457 ] && ch=10
[ "$freq" = 2.467 ] && ch=12

echo 'Channel   : '$ch
echo ''

#ethernet
ethernet=''
for i in `ifconfig|grep HWaddr|awk '{print $1}'`
do
	iwconfig $i 2>&1|grep "no wireless" > /dev/null && ethernet="$i $ethernet"
done

#school
school=""
school=`ping -c 1 schoolserver 2>&1|sed -n 's/^.*bytes from \([^ ]*\) .*$/\1/p'`
echo "School    : $school"

#config
eth=''
msh=''
ipeth=''
ipmsh=''

for i in `ifconfig|grep HWaddr|grep ^eth|awk '{print $1}'`
do
	ifconfig $i|grep "inet addr" > /dev/null && eth=$i
done

for i in `ifconfig|grep HWaddr|grep ^msh|awk '{print $1}'`
do
	ifconfig $i 2>&1|grep "inet addr" > /dev/null && msh=$i
done

[ -n "$eth" ] && ipeth=`ifconfig $eth|sed -n '/t addr/s/^[^:]*:\([^ ]*\) .*$/\1/p'`
[ -n "$msh" ] && ipmsh=`ifconfig $msh|sed -n '/t addr/s/^[^:]*:\([^ ]*\) .*$/\1/p'`

config=''
if [ -n "$ipeth" ]
then 
	echo $ethernet|grep $eth > /dev/null && config="Ethernet"
	echo $ethernet|grep $eth > /dev/null || config="Simple Wifi"
	[ -n "$school" ] && config=`echo "$config"|sed s/Simple/School/`	
elif [ -n "$dns" ]
then
	config="Simple Mesh"
elif [ "${ipmsh:0:3}" = "169" ]
then
	config='MPP'
else config='School Mesh'
fi	

#acting as MPP
asmpp='' 
[ -n "$ipmsh" ] && cat /etc/dhcpd.conf 2>&1|grep "$ipmsh" > /dev/null && asmpp=' - Acting as MPP'



echo "Config    : $config$asmpp"
[ -n "$ethernet" ] && echo "Ethernet  : $ethernet"
echo ''
}

function help {

echo "Usage: olpc-netstatus [-i|-n]"
echo
echo "  -i     device info only"
echo "  -n     network info only"
echo
}

if [ "$1" = "-i" ];then info;echo
elif [ "$1" = "-n" ];then net
elif [ -n "$1" ];then help;
else info;net
fi
